/**
 * 
 */
package com.jzcodecheck.ui.core.action;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.KeyStroke;

import com.jzcodecheck.ui.core.BatchUI;
import com.jzcodecheck.ui.core.processor.codescan.CodeNoteScanProcessor;

/**
 * 代码扫码Action
 * 作者:龙色波
 * 日期:2018-12-14
 */
public class CodeNoteScanAction extends AbstractUIAction {

	private String path;

	public CodeNoteScanAction(BatchUI ui) {
		super(ui);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -3882032380476779079L;

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser fc=new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//只能选择目录
		File f=null;
		int flag=0;
		try{
			flag=fc.showOpenDialog(null); 
		} 
		catch(HeadlessException head){ 
			System.out.println("Open File Dialog ERROR!");
			return;
		} 
		if(flag==JFileChooser.APPROVE_OPTION){
			//获得该文件 
			f=fc.getSelectedFile(); 
			path=f.getPath();
			//启动扫描线程
			Thread t = new Thread(new Runnable(){  
	            public void run(){  
	           // run方法具体重写
	    			//执行扫描
	    			CodeNoteScanProcessor codeNoteScanProcessor = new CodeNoteScanProcessor(ui);
	    			try {
	    				ui.refreshScanResult(codeNoteScanProcessor.doScan(path));
	    			} catch (Exception e1) {
	    				e1.printStackTrace();
	    			}	      
	        }});  
	        t.start();  

		} 
		
//		FileDialog dlg;
//		dlg = new FileDialog(ui.getFrame(),UIPropertyHelper.getString("dialog.codenotescan.title"),FileDialog.LOAD);
//		dlg.setFilenameFilter(new XmlFilenameFilter());
//		dlg.setVisible(true);
//		String fileName;
//		fileName = dlg.getDirectory()+dlg.getFile();
//		if(dlg.getFile()!=null){			
//			try {
//				//TODO:执行扫描
//				CodeNoteScanProcessor codeNoteScanProcessor = new CodeNoteScanProcessor(ui.getOutputTextArea());
//				codeNoteScanProcessor.doScan(fileName);
//			} catch (Exception e1) {
//				logger.error(e1.getMessage(), e1);
//				e1.printStackTrace();
//			}
//		}

	}
	public CodeNoteScanAction(String text, Icon icon, char accelerator, String description) {
		    super(text, icon);

		    putValue("AcceleratorKey", KeyStroke.getKeyStroke(accelerator));

		    putValue("ShortDescription", description);
		  }
}
